<?php
    header('Content-type:text/json');
    require "../connection.php";
    $order_id = $_POST['order_id'];
   //查询这次订单总金额 pay_price
   $sql = "select pay_price from orders where id = '$order_id'";
   $res = $conn->doSql($sql);
   $pay_price = $res[0]['pay_price'];
    // 扣除用户余额
    // 1. 查询该订单用户
    $sql = "select user_id from orders where id = '$order_id'";
    $res = $conn->doSql($sql);
    $user_id = $res[0]['user_id'];
    // 2. 查询并判断用户余额 是否足够 user_balance
    $sql = "select user_balance from user where id='$user_id'";
    $res = $conn->doSql($sql);
    $user_balance = $res[0]['user_balance'];
    // 3.1  修改用户余额
    if($user_balance>=$pay_price){
        // 执行支付
        $remain = $user_balance-$pay_price;
        $sql = "update user set user_balance='$remain' where id ='$user_id'";
        $result = $conn->doSql($sql);
        // 修改订单信息  支付和支付时间
        $pay_time = date("Y-m-d H:i:s",time());
        $sql = "update orders set is_pay=1,pay_time='$pay_time',update_time='$pay_time' where id='$order_id'";
        $res = $conn->doSql($sql);
    }else{
        //3.2 支付失败，用户余额不足
        $result = 0;
    }
    echo json_encode($result);

?>